package chatclient.applets;

import chatclient.netio.Attr;
import chatclient.netio.AttrList;
import chatclient.applets.components.Border;
import chatclient.tools.ChatException;
import chatclient.tools.ChatListener;
import chatclient.netio.ClientAttr;
import chatclient.netio.Command;
import chatclient.netio.Event;
import chatclient.tools.Filter;
import chatclient.netio.Member;
import chatclient.applets.note.Note;
import chatclient.applets.components.Button;
import chatclient.tools.NoteContainer;
import chatclient.tools.NoteListener;
import chatclient.netio.Perms;
import chatclient.tools.QSort;
import chatclient.applets.components.Scrollbar;
import java.awt.Adjustable;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class ChatView extends JApplet
        implements ChatListener, NoteContainer {

    static final String copyright = Chat.copyrightInfo;
    private String status;
    private Chat chat;
    protected String suspendKey;
    private String myChannel;
    private AttrList myChannelInfo;
    private Hashtable members;
    private boolean fixMembers;
    private Vector memberKeys;
    private Hashtable memberHash;
    private static String KEYSEP = "\0";
    private Hashtable colorCache;
    private Hashtable transparentCache;
    private Border memberBorder;
    private long imageRate;
    private Double minColorDistance;
    private Map<String, BufferedImage> loadedImages;
    protected boolean IAmHidden;
    protected ChatPanel chatPanel;
    public final String smileyDir = "smilies";
    public final String borderDir = "borders";
    public final String buttonDir = "buttons";
    public final String scrollbarDir = "scrollbars";
    public final String backgroundDir = "backgrounds";

    @Override
    public void init() {

        try {
            if (Chat.debug) {
                System.err.println((new StringBuilder("Init started ")).append(this).toString());
            }

            IAmHidden = false;

            loadedImages = new HashMap<String, BufferedImage>();
            status = Chat.copyrightInfo;
            members = new Hashtable();
            memberKeys = new Vector();
            memberHash = new Hashtable();

            chat = Chat.findChat(getParameter("instance"));
            if (chat == null) {
                System.err.println("no chat!");
            }
            suspendKey = getParameter("suspendKey");

            initChatPanel();


            colorCache = new Hashtable();
            transparentCache = new Hashtable();

            String s;
            imageRate = (s = getParameter("imageRate")) != null ? Integer.parseInt(s) : 100L;
            minColorDistance = (s = getParameter("minColorDistance")) != null ? Double.valueOf(s) : null;
            memberBorder = (s = getParameter("memberBorder")) != null ? createBorder(s) : null;

            try {
                SwingUtilities.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {

                        chatPanel.initChatView();
                        setContentPane(chatPanel);
                    }
                });
            } catch (InvocationTargetException ex) {
                ex.getTargetException().printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("chatPanel.initChatView() didn't complete successfully");
            }

            initChat();

            if (Chat.debug) {
                System.err.println((new StringBuilder("Init finished ")).append(this).toString());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void initChatPanel() {
        chatPanel = new ChatPanel(this);
    }

    protected void initChat() {
        Vector vector = null;
        try {
            vector = (Vector) chat.recallState(getSuspendKey());
        } catch (Exception e) {
            vector = null;
        }

        if (vector == null) {
            initState();
        } else {
            restoreState(vector.elements());
        }

        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    chatPanel.updateBackground();
                    chatPanel.updateHeader();
                }
            });
        } catch (InvocationTargetException ex) {
            ex.getTargetException().printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("chatPanel.updateBackground() didn't complete successfully");
        }

        chat.addChatListener(this, getSuspendKey());
    }

    @Override
    public void destroy() {
        try {
            if (Chat.debug) {
                System.err.println((new StringBuilder("Destroy started ")).append(this).toString());
            }

            cleanupChat(true);

            try {
                SwingUtilities.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {
                        chatPanel.cleanupChatView();
                    }
                });
            } catch (InvocationTargetException ex) {
                ex.getTargetException().printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(" chatPanel.cleanupChatView() didn't complete successfully");
            }

            if (Chat.debug) {
                System.err.println((new StringBuilder("Destroy finished ")).append(this).toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start() {
        try {
            if (Chat.debug) {
                System.err.println((new StringBuilder("Start ")).append(this).toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        try {
            if (Chat.debug) {
                System.err.println((new StringBuilder("Stop ")).append(this).toString());
            }

            cleanupChat(true);

            try {
                SwingUtilities.invokeAndWait(new Runnable() {

                    @Override
                    public void run() {
                        chatPanel.cleanupChatView();
                    }
                });
            } catch (InvocationTargetException ex) {
                ex.getTargetException().printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(" chatPanel.cleanupChatView() didn't complete successfully");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void cleanupChat(boolean flag) {
        if (flag) {
            chat.removeChatListener(this, getSuspendKey());
        }
        Vector vector = new Vector();
        saveState(vector);
        chat.saveState(getSuspendKey(), vector);

        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    chatPanel.setVisible(false);
                }
            });
        } catch (Exception e) {
            System.err.println("chatPanel.setVisible() didn't complete successfully");
        }
    }

    public void setStatus(Throwable throwable) {
        showStatus(status = throwable.toString());
        throwable.printStackTrace();
        getContentPane().repaint();
    }

    @Override
    public Chat getChat() {
        return chat;
    }

    @Override
    public String getAppletInfo() {
        return Chat.copyrightInfo;
    }

    @Override
    public String[][] getParameterInfo() {
        return new String[0][];
    }

    @Override
    public Filter[] getFilters() {
        return chat.getFilters();
    }

    @Override
    public Double getMinColorDistance() {
        return minColorDistance;
    }

    private Color getColorFromParameter(String s) {
        String s1 = getParameter(s);
        if (s1 != null) {
            return Color.decode(s1);
        } else {
            return null;
        }
    }

    protected Color getColor(String s, String as[], int i) {
        StringBuilder stringbuffer = new StringBuilder();
        for (int j = i; j < as.length; j++) {
            if (as[j] != null) {
                stringbuffer.append(stringbuffer.length() > 0 ? (new StringBuilder(String.valueOf(Character.toUpperCase(as[j].charAt(0))))).append(as[j].substring(1)).toString() : as[j]);
            }
        }

        stringbuffer.append(stringbuffer.length() > 0 ? (new StringBuilder(String.valueOf(Character.toUpperCase(s.charAt(0))))).append(s.substring(1)).toString() : s);
        String s1 = stringbuffer.toString();
        Color color = (Color) colorCache.get(s1);
        if (color == null && transparentCache.get(s1) == null) {
            color = getColorFromParameter(s1);
            if (color == null && i < as.length) {
                color = getColor(s, as, i + 1);
            }
            if (color == null) {
                transparentCache.put(s1, s1);
            } else {
                colorCache.put(s1, color);
            }
        }
        return color;
    }

    protected Color getColor(String s, String as[]) {
        return getColor(s, as, 0);
    }

    protected Color getColor(String s, String s1) {
        return getColor(s, new String[]{
                    s1
                }, 0);
    }

    protected Color getColor(String s) {
        return getColor(s, new String[0], 0);
    }

    @Override
    public Color getBackground(String s) {
        return getColor("bgcolor", new String[]{
                    s
                }, 0);
    }

    @Override
    public Color getForeground(String s) {
        return getColor("fgcolor", new String[]{
                    s
                }, 0);
    }

    @Override
    public Color getNickColor(String s, String s1) {

        String s2 = chat.getSelectedNick();
        if (s == null) {
            return null;
        }

        if (chat.isIgnoreNick(s)) {
            return getColor("fgcolor", new String[]{
                        s1, "ignore"
                    });
        }

        Color color;
        if (s2 != null && s2.equalsIgnoreCase(s) && (color = getColor("fgcolor", new String[]{
                    s1, "select"
                })) != null) {
            return color;
        }


        Member member;
        if ((member = getMember(s)) != null && member.isMarked()) {
            return getColor("fgcolor", new String[]{
                        s1, "mark"
                    });
        }

        return null;

    }

    @Override
    public Color getNickBackground(String s, String s1) {
        String s2 = chat.getSelectedNick();
        if (s == null) {
            return null;
        }
        if (s2 != null && s2.equalsIgnoreCase(s)) {
            return getColor("bgcolor", new String[]{
                        s1, "select"
                    });
        }
        Member member;
        if ((member = getMember(s)) != null && member.isMarked()) {
            return getColor("bgcolor", new String[]{
                        s1, "mark"
                    });
        }
        if (chat.isIgnoreNick(s)) {
            return getColor("bgcolor", new String[]{
                        s1, "ignore"
                    });
        } else {
            return null;
        }
    }

    @Override
    public Color getChannelColor(String s, String s1) {
        if (myChannel == null || s == null || myChannel.equalsIgnoreCase(s)) {
            return null;
        } else {
            return getColor("fgcolor", new String[]{
                        s1, "inactive"
                    });
        }
    }

    @Override
    public Color getChannelBackground(String s, String s1) {
        if (myChannel == null || s == null || myChannel.equalsIgnoreCase(s)) {
            return null;
        } else {
            return getColor("bgcolor", new String[]{
                        s1, "inactive"
                    });
        }
    }

    @Override
    public long getImageRate() {
        return imageRate;
    }

    public String getSuspendKey() {
        return suspendKey;
    }

    public void clearState() {
        myChannel = null;
        myChannelInfo = new AttrList();
        members = new Hashtable();
        fixMembers = false;
        refreshedMembers();
    }

    public void initState() {
        initMyState();
    }

    private void initMyState() {
        myChannel = chat.getMyChannel();
        myChannelInfo = chat.getMyChannelInfo();
        members = new Hashtable();
        fixMembers = false;
        refreshedMembers();
    }

    public void saveState(Vector vector) {
        vector.addElement(myChannel);
        vector.addElement(myChannelInfo);
        vector.addElement(members);
        vector.addElement(fixMembers);
    }

    public void restoreState(Enumeration enumeration) {
        myChannel = (String) enumeration.nextElement();
        myChannelInfo = (AttrList) enumeration.nextElement();
        members = (Hashtable) enumeration.nextElement();
        fixMembers = ((Boolean) enumeration.nextElement()).booleanValue();
        refreshedMembers();
    }

    public String getMyName() {
        return chat.getMyName();
    }

    public String getMyChannel() {
        return myChannel;
    }

    public AttrList getMyChannelInfo() {
        return myChannelInfo;
    }

    public Member getMember(String s) {
        return (Member) members.get(s.toLowerCase());
    }

    public Enumeration getMemberEnum() {
        return members.elements();
    }

    public boolean followNickList() {
        return false;
    }

    @Override
    public String unlisten() {
        cleanupChat(false);
        return getSuspendKey();
    }

    @Override
    public void reinitialize() {
        initState();
        if (myChannel != null) {
            try {
                AttrList attrlist = new AttrList();
                attrlist.add(new ClientAttr(ClientAttr.CHANNELNAME, myChannel));
                getChat().sendCommand(new Command(Command.NICKLIST, attrlist));
                getChat().sendCommand(new Command(Command.GETINFO, attrlist));
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        doInternalEvent("historylost", new AttrList());
    }

    @Override
    public void changedActiveChannel() {
        initMyState();
    }

    @Override
    public void doEvent(Event event, boolean ignored) {
        AttrList attrlist = event.getParams();
        ClientAttr nicknameAttr = (ClientAttr) attrlist.get(ClientAttr.NICKNAME);
        String nickname = null;
        ClientAttr targetNameAttr = (ClientAttr) attrlist.get(ClientAttr.TARGETNAME);
        String targetName = null;
        ClientAttr channelNameAttr = (ClientAttr) attrlist.get(ClientAttr.CHANNELNAME);
        String channelName = null;
        try {
            nickname = nicknameAttr != null ? nicknameAttr.getString() : null;
        } catch (Exception exception) {
        }
        try {
            targetName = targetNameAttr != null ? targetNameAttr.getString() : null;
        } catch (Exception exception1) {
        }
        try {
            channelName = channelNameAttr != null ? channelNameAttr.getString() : null;
        } catch (Exception exception2) {
        }
        boolean isMeSource = getMyName() != null && nickname != null && nickname.equalsIgnoreCase(getMyName());
        boolean isMeTarget = getMyName() != null && targetName != null && targetName.equalsIgnoreCase(getMyName());
        boolean isMyActiveChannel = getMyChannel() != null && channelName != null && channelName.equalsIgnoreCase(getMyChannel());
        doEvent(event, ignored, attrlist, nicknameAttr != null, nickname, isMeSource, targetNameAttr != null, targetName, isMeTarget, channelNameAttr != null, channelName, isMyActiveChannel);
    }

    protected void doEvent(Event event, boolean ignored, AttrList attrlist, boolean isNicknameSet, String nickname, boolean isMeSource, boolean isTargetNameSet,
            String targetName, boolean isMeTarget, boolean isChannelNameSet, String channelName, boolean isMyActiveChannel) {

        byte eventId = event.getId();

        switch (eventId) {
            case Event.HIDEON:
                IAmHidden = true;
                refreshedMembers();
                break;
            case Event.HIDEOFF:
                IAmHidden = false;               
                refreshedMembers();
                break;
            default:
                break;
        }

        if (eventId == Event.NICKLIST || eventId == Event.FOUNDNICKS || !fixMembers && (isMyActiveChannel || !isChannelNameSet && getMyChannel() == null)) {
            String lcNickname = nickname != null ? nickname.toLowerCase() : null;
            try {
                switch (eventId) {
                    case Event.CHANNELLIST:
                    case Event.BANLIST:
                    case Event.E12:
                    case Event.VARIABLE:
                    case Event.PERMS:
                    case Event.NOTSIGNEDUP:
                    case Event.ISSIGNEDUP:
                    case Event.NICKINUSE:
                    case Event.NICKRESERVED:
                    case Event.BANNEDONSERVER:
                    case Event.BANNEDONCHANNEL:
                    case Event.NOPERMISSION:
                    case Event.FLOODING:
                    case Event.QUEUEPOSITION:
                    case Event.QUEUEUPDATE:
                    case Event.KILLED:
                    case Event.BANNED:
                    case Event.UNBANNED:
                    case Event.ENQUEUED:
                    case Event.DEQUEUED:
                    case Event.PROXYLIST:
                    case Event.NOTARGET:
                    case Event.TOOMANYIPCXNS:
                    case Event.NOCHANNEL:
                    default:
                        break;

                    case Event.JOINED: {
                        if (getMyName().equals(nickname)) {
                            changedActiveChannel();
                        }
                        myChannelInfo = chat.getChannelInfo(myChannel);
                        Member member = new Member(nickname, (AttrList) attrlist.clone());
                        Member member5 = (Member) members.put(lcNickname, member);
                        if (member5 != null) {
                            removedMember(member5);
                        }
                        addedMember(member);
                        chatPanel.updateHeader();

                        break;
                    }
                    case Event.PARTED: {
                        if (getMyName().equals(nickname)) {
                            changedActiveChannel();
                        }
                        myChannelInfo = chat.getChannelInfo(myChannel);
                        Member member1 = (Member) members.remove(lcNickname);
                        if (member1 != null) {
                            removedMember(member1);
                        }

                        chatPanel.updateHeader();

                        break;
                    }
                    case Event.NICKLIST:
                    case Event.FOUNDNICKS: {
                        AttrList attrlist1 = new AttrList();
                        members.clear();
                        Attr attr;
                        for (Enumeration enumeration = attrlist.getEnumeration(); enumeration.hasMoreElements(); attrlist1.add(attr)) {
                            attr = (Attr) enumeration.nextElement();
                            if (attr.getId() == ClientAttr.NICKNAME) {
                                String s6 = attr.getString();
                                String s7 = s6.toLowerCase();
                                attrlist1 = new AttrList();
                                members.put(s7, new Member(s6, attrlist1));
                            }
                        }

                        myChannelInfo = new AttrList();
                        myChannelInfo.set(new ClientAttr(ClientAttr.MEMBERCOUNT, members.size()));

                        Attr aChatterCount = attrlist.get(ClientAttr.CHATTERCOUNT);
                        if (aChatterCount != null) {
                            myChannelInfo.set(new ClientAttr(ClientAttr.CHATTERCOUNT, aChatterCount.getInt()));
                        }
                        fixMembers = eventId == Event.FOUNDNICKS;
                        myChannel = channelName;
                        refreshedMembers();
                        chatPanel.updateHeader();

                        break;
                    }
                    case Event.GAVEPERMS:
                    case Event.TOOKPERMS: {
                        if (targetName == null) {
                            break;
                        }
                        String lcTargetName = targetName.toLowerCase();
                        Member targetMember = (Member) members.get(lcTargetName);
                        if (targetMember == null) {
                            break;
                        }
                        Perms perms = targetMember.getPerms();
                        Attr attr1 = attrlist.get(ClientAttr.PERMS);
                        Perms perms1 = Perms.fromAttr(attr1);
                        switch (eventId) {
                            case Event.GAVEPERMS:
                                perms1 = perms.set(perms1);
                                break;

                            case Event.TOOKPERMS:
                                perms1 = perms.clear(perms1);
                                break;
                        }
                        targetMember.setPerms(perms1);
                        updatedMember(targetMember);
                        break;
                    }
                    case Event.SETPROFILE: {
                        if (targetName == null) {
                            break;
                        }
                        String lcTargetName = targetName.toLowerCase();
                        Member targetMember = (Member) members.get(lcTargetName);
                        if (targetMember != null) {
                            targetMember.setProfile(attrlist.get(ClientAttr.PROFILE).getString());
                            updatedMember(targetMember);
                        }
                        break;
                    }
                    case Event.PROFILE: {
                        if (nickname == null) {
                            break;
                        }
                        Member member = (Member) members.get(lcNickname);
                        if (member != null) {
                            member.setProfile(attrlist.get(ClientAttr.PROFILE).getString());
                            updatedMember(member);
                        }
                        break;
                    }
                    case Event.MEMBERINFO: {
                        if (nickname == null) {
                            break;
                        }
                        Member member = (Member) members.get(lcNickname);
                        if (member != null) {
                            member.setMemberInfo((Attr[]) attrlist.toArray(ClientAttr.MEMBERINFO));
                            updatedMember(member);
                        }
                        break;
                    }
                    case Event.USERINFO: {
                        Member member = (Member) members.get(lcNickname);
                        if (member != null) {
                            member.setUserInfo((Attr[]) attrlist.toArray(Event.USERINFO));
                            updatedMember(member);
                        }
                        break;
                    }
                    case Event.CHANNELINFO: {
                        myChannelInfo = attrlist;

                        chatPanel.updateBackground();
                        chatPanel.updateHeader();

                        break;
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }

    @Override
    public void doInternalEvent(String s1, AttrList attrlist1) {
    }

    @Override
    public void startedPrivate(String s1) {
    }

    @Override
    public void endedPrivate(String s1) {
    }

    @Override
    public void selectedNick(String s) {
        getContentPane().repaint();
    }

    @Override
    public void deselectedNick() {
        getContentPane().repaint();
    }

    @Override
    public void clearedNotes(String s) {
        getContentPane().repaint();
    }

    @Override
    public void updated() {
        getContentPane().repaint();
    }

    @Override
    public void updatedNick(String s) {
        Member member = getMember(s);
        if (member != null) {
            updatedMember(member);
        }
    }

    @Override
    public void updatedMembers() {
        refreshedMembers();
    }

    public void updatedBgimage(Image image1) {
    }

    public void addedMember(Member member1, int j) {
    }

    public void removedMember(Member member1, int j) {
    }

    public void updatedMember(Member member1, int k, int l) {
    }

    protected Button createButton(String s) {
        Image image = getButtonImage((new StringBuilder(String.valueOf(s))).append("Released").toString());
        Image image1 = getButtonImage((new StringBuilder(String.valueOf(s))).append("Pressed").toString());
        Image image2 = getButtonImage((new StringBuilder(String.valueOf(s))).append("Hovered").toString());

        return new Button(image, image1, image2);
    }

    protected Border createBorder(String s) {
        Image image = getBorderImage((new StringBuilder(String.valueOf(s))).append("TopLeft").toString());
        Image image1 = getBorderImage((new StringBuilder(String.valueOf(s))).append("Left").toString());
        Image image2 = getBorderImage((new StringBuilder(String.valueOf(s))).append("BottomLeft").toString());
        Image image3 = getBorderImage((new StringBuilder(String.valueOf(s))).append("Top").toString());
        Image image4 = getBorderImage((new StringBuilder(String.valueOf(s))).append("Bottom").toString());
        Image image5 = getBorderImage((new StringBuilder(String.valueOf(s))).append("TopRight").toString());
        Image image6 = getBorderImage((new StringBuilder(String.valueOf(s))).append("Right").toString());
        Image image7 = getBorderImage((new StringBuilder(String.valueOf(s))).append("BottomRight").toString());
        return new Border(image, image1, image2, image3, image4, image5, image6, image7);
    }

    protected Adjustable createScrollbar() {
        Button button = null;
        Button button1 = null;
        Image image = null;
        Image image1 = null;
        Image image2 = null;
        Image image3 = null;
        try {
            button = createScrollbarButton("top");
            button1 = createScrollbarButton("bottom");
            image = getScrollbarImageX("thumbTop");
            image1 = getScrollbarImageX("thumbBottom");
            image2 = getScrollbarImageX("thumb");
            image3 = getScrollbarImageX("bar");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return new Scrollbar(1, 0, 1, 0, 1, image3, image2, image, image1, button, button1);
    }

    protected Button createScrollbarButton(String s) {
        Image image = getScrollbarImageX((new StringBuilder(String.valueOf(s))).append("Released").toString());
        Image image1 = getScrollbarImageX((new StringBuilder(String.valueOf(s))).append("Pressed").toString());
        Image image2 = getScrollbarImageX((new StringBuilder(String.valueOf(s))).append("Hovered").toString());

        return new Button(image, image1, image2);

    }

    public BufferedImage getScrollbarImageX(String name) {
        return getScrollbarImage(getParameter(name));
    }

    public BufferedImage getBackgroundImage(String name) {
        return getImage("backgrounds", name, getParameter("bgExtension"));
    }

    public BufferedImage getScrollbarImage(String name) {
        return getImage("scrollbars", name);
    }

    @Override
    public BufferedImage getSmileyImage(String name) {
        System.out.println("smiley name="+name);
        return getImage("smilies", name);
    }

    public BufferedImage getButtonImage(String name) {
        return getImage("buttons", name);
    }

    public BufferedImage getBorderImage(String name) {
        return getImage("borders", name);
    }

    public BufferedImage getImage(String folder, String name) {
        return getImage(folder, name, ".gif");
    }

    public BufferedImage getImage(String folder, String name, String fileExtension) {
        if (name == null || name.length() == 0) {
            return null;
        }
        try {

            String fullname = folder + "/" + name + (fileExtension != null ? fileExtension : "");
            if (loadedImages.containsKey(fullname)) {
                return loadedImages.get(fullname);
            }

            boolean useHttp = false;
            
            String tmp;
            if ((tmp = getParameter(folder)) != null && tmp.length() > 0) {
                folder = tmp;
                useHttp = true;
            }
            if (useHttp && (tmp = getParameter("images")) != null && tmp.length() > 0) {
                folder = (new StringBuilder(String.valueOf(tmp))).append("/").append(folder).toString();
            }

            if (useHttp) {
                URL url = new URL(getCodeBase(), (new StringBuilder(String.valueOf(folder))).append("/").append(name).append(fileExtension != null ? fileExtension : "").toString());
                BufferedImage image = ImageIO.read(url);
                loadedImages.put(fullname, image);
                return image;
            }

            String sUrl = "/chatclient/images/" + folder + "/" + name + (fileExtension != null ? fileExtension : "");

            InputStream is = getClass().getResourceAsStream(sUrl);
            BufferedInputStream bis = new BufferedInputStream(is);

            BufferedImage image = ImageIO.read(bis);
            loadedImages.put(fullname, image);
            return image;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

    public Note createMemberNote(NoteListener notelistener, Member member) {
        if (notelistener == null || member == null) {
            return null;
        }
        String parMember = getParameter("member");
        AttrList attrlist = member.getAttrs();
        ClientAttr nicknameAttr = (ClientAttr) attrlist.get(ClientAttr.NICKNAME);
        String nickname = null;
        try {
            nickname = nicknameAttr != null ? nicknameAttr.getString() : null;
        } catch (Exception exception) {
        }
        Note note = Note.fromTemplate(notelistener, this, size().width, parMember, nickname, attrlist, getFont(), getFontMetrics(getFont()));
        if (note != null && memberBorder != null) {
            note.setBorder(memberBorder);
        }
        return note;
    }

    public String getMemberParameter(Member member, String s, String s1) {
        AttrList attrlist = member.getAttrs();
        String s3 = getParameter(s);
        if (s3 == null) {
            s3 = s1;
        }
        Perms perms;
        try {
            Attr attr = attrlist.get(ClientAttr.PERMS);
            perms = attr == null ? new Perms() : Perms.fromAttr(attr);
        } catch (Exception exception) {
            perms = new Perms();
        }
        String s2;
        int i;
        for (i = 0; (s2 = getParameter((new StringBuilder(String.valueOf(s))).append(i).toString())) != null; i++) {
            int j = s2.indexOf(':');
            String s4 = j >= 0 ? s2.substring(0, j) : s2;
            if (s4.length() != 0 && !matchPerms(perms, s4)) {
                continue;
            }
            if (j >= 0) {
                s3 = s2.substring(j + 1);
            }
            break;
        }

        return s3 != null ? s3 : String.valueOf((char) (i < 10 ? 48 + i : (97 + i) - 10));
    }

    private boolean matchPerms(Perms perms, String s) {
        int j;
        for (int i = 0; i < s.length(); i = j + 1) {
            j = s.indexOf('|', i);
            if (j == -1) {
                j = s.length();
            }
            Perms perms1 = new Perms(s.substring(i, j));
            if (perms.mask(perms1).equals(perms1)) {
                return true;
            }
        }

        return false;
    }

    private int find(Vector vector, String s) {
        return find(vector, s, 0, vector.size());
    }

    private int find(Vector vector, String s, int i, int j) {
        if (i >= j) {
            return i;
        }
        int k = i + (j - i) / 2;
        int l = s.compareTo((String) vector.elementAt(k));
        if (l == 0) {
            return k;
        }
        if (l < 0) {
            return find(vector, s, i, k);
        } else {
            return find(vector, s, k + 1, j);
        }
    }

    public String getMemberOrder(Member member) {
        return "";
    }

    public Enumeration getOrderedMemberEnum() {
        return new Enumeration() {

            int index = 0;

            @Override
            public boolean hasMoreElements() {
                return this.index < ChatView.this.memberKeys.size();
            }

            @Override
            public Object nextElement() {
                return ChatView.this.getMember(this.index++);
            }
        };
    }

    public Member getMember(int i) {
        if (i >= 0 && i < memberKeys.size()) {
            String s = (String) memberKeys.elementAt(i);
            return getMember(s.substring(s.indexOf(KEYSEP) + 1));
        } else {
            return null;
        }
    }

    public int findMember(String s) {
        return find(memberKeys, (new StringBuilder(String.valueOf(getMemberOrder(new Member())))).append(KEYSEP).append(s.toLowerCase()).toString());
    }

    public void addedMember(Member member) {
        String lcName = member.getName().toLowerCase();
        String s1 = (new StringBuilder(String.valueOf(getMemberOrder(member)))).append(KEYSEP).append(lcName).toString();
        int i = find(memberKeys, s1);
        memberKeys.insertElementAt(s1, i);
        memberHash.put(lcName, s1);
        addedMember(member, i);
    }

    public void removedMember(Member member) {
        String lcName = member.getName().toLowerCase();
        String s1 = (String) memberHash.get(lcName);
        int i = find(memberKeys, s1);
        memberKeys.removeElementAt(i);
        memberHash.remove(lcName);
        removedMember(member, i);
    }

    public void updatedMember(Member member) {
        String lcName = member.getName().toLowerCase();
        String s1 = (String) memberHash.get(lcName);
        String s2 = (new StringBuilder(String.valueOf(getMemberOrder(member)))).append(KEYSEP).append(lcName).toString();
        int i = find(memberKeys, s1);
        memberKeys.removeElementAt(i);
        int j = find(memberKeys, s2);
        memberKeys.insertElementAt(s2, j);
        memberHash.put(lcName, s2);
        updatedMember(member, i, j);
    }

    public void refreshedMembers() {
        memberKeys.removeAllElements();
        memberHash.clear();
        String s;
        String s1;
        for (Enumeration enumeration = getMemberEnum(); enumeration.hasMoreElements(); memberHash.put(s, s1)) {
            Member member = (Member) enumeration.nextElement();
            s = member.getName().toLowerCase();
            s1 = (new StringBuilder(String.valueOf(getMemberOrder(member)))).append(KEYSEP).append(s).toString();
            memberKeys.addElement(s1);
        }

        (new QSort()).sort(memberKeys);
    }
}
